//
//  UserViewController.m
//  QYer
//
//  Created by lanou on 16/8/5.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "UserViewController.h"
#import "UserTableViewCell.h"
#import "UserSetUpViewController.h"

@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *userArray;

@end

@implementation UserViewController

- (void)createSubViews {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([UserTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"reuse"];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 300)];
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headerImage.jpg"]];
    self.imageView.frame = self.headerView.frame;
    self.imageView.layer.masksToBounds = YES;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.headerView addSubview:self.imageView];
    self.tableView.tableHeaderView = self.headerView;
    [self createButton];
}

//三个按钮
- (void)createButton {
    UIView *userView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, kScreenWidth, 100)];
    userView.backgroundColor = [UIColor lightGrayColor];
    userView.alpha = 0.7;
    [self.headerView addSubview:userView];
    
    NSArray *buttonArray = @[@"收藏的帖子", @"我的锦囊", @"我的行程"];
    CGFloat buttonW = (kScreenWidth - 240) / 3;
    CGFloat buttonH = 80;
    for (int i = 0; i < buttonArray.count; i++) {
        CustomButton *button = [[CustomButton alloc] init];
        button.tag = 200+i;
        button.frame = CGRectMake(i*(buttonW+80)+40, 10, buttonW, buttonH);
        
        button.backgroundColor = [UIColor clearColor];
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:buttonArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:buttonArray[i]] forState:UIControlStateNormal];
        [userView addSubview:button];
        
    }
}



#pragma mark -----

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createSubViews];
    //折扣
    NSArray *discountArray = @[@"我的订单", @"我收藏的折扣券", @"我的优惠券"];
    //足迹
    NSArray *destinationArray = @[@"我收藏的目的地", @"我的足迹", @"等我点评的目的地"];
    //问答
    NSArray *postArray = @[@"我发布的帖子", @"我的问答", @"我的结伴"];
    
    self.userArray = @[discountArray, destinationArray, postArray];
    
    //NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    //NSLog(@"登录用户名:%@", string);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"提醒"] style:(UIBarButtonItemStylePlain) target:self action:@selector(toRemindVC)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"设置"] style:(UIBarButtonItemStylePlain) target:self action:@selector(toSetUpVC)];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)buttonClick:(UIButton *)button {
    NSLog(@"%@", button.titleLabel.text);
    if (button.tag == 200) {
        //收藏的帖子
    } else if (button.tag == 201) {
        //我的锦囊
    } else {
        //我的行程
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"username"]) {
            //已经登录状态
            NSLog(@"已经登录过!");
            DetailViewController *detailVC = [[DetailViewController alloc] init];
            detailVC.urlString = @"http://m.qyer.com/plan/appmylist?from_device=app&client_id=qyer_ios&oauth_token=64ae5bc0d023f71444c8f00f096eae24";
            [self.navigationController pushViewController:detailVC animated:YES];
        } else {
            [self toLoginVC];
        }
    }
}

//设置
- (void)toSetUpVC {
    UserSetUpViewController *setupVC = [[UserSetUpViewController alloc] init];
    [self.navigationController pushViewController:setupVC animated:YES];
}

//提醒
- (void)toRemindVC {
    NSLog(@"提醒!");
}

//确认是否登陆
- (void)toLoginVC {
    UINavigationController *naVC = [[UIStoryboard storyboardWithName:@"LoginAndRegist" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginAndRegist"];
    [self presentViewController:naVC animated:YES completion:nil];
}


#pragma mark ----- UITableViewDataSource -----

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.userArray.count;
}

//分区内行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.userArray[section] count];
}

//Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.titleLabel.text = self.userArray[indexPath.section][indexPath.row];
    return cell;
}

//分区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

//行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

//Cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"第%ld分区第%ld行: %@", indexPath.section, indexPath.row, self.userArray[indexPath.section][indexPath.row]);
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"username"]) {
        //已经登录状态
        NSLog(@"已经登录过!");
        
    } else {
        [self toLoginVC];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.tableView.contentOffset.y < 0) {
        self.imageView.frame = CGRectMake(0, self.tableView.contentOffset.y, self.view.frame.size.width, 300-self.tableView.contentOffset.y);
    }
}




@end
